﻿using Common.Device.Enums;
using Common.Device.Models;
using Common.Tool.DataTools;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Common.Device.Views
{
    /// <summary>
    /// CaiJiMoKuaiView.xaml 的交互逻辑
    /// </summary>
    public partial class CaiJiMoKuaiView : UserControl
    {
        private ViewModels.CaiJiMoKuaiViewModel viewModel = new ViewModels.CaiJiMoKuaiViewModel();
        public CaiJiMoKuaiView(DeviceEnum.DeviceModelEnum model, Order order = null)
        {
            InitializeComponent();
            //根据仪器型号初始化界面数据
            switch (model)
            {
                case DeviceEnum.DeviceModelEnum.RSWSN012D:
                    viewModel.Channels = new System.Collections.ObjectModel.ObservableCollection<string> { "温度","湿度" };
                    viewModel.Channel = "温度";
                    break;
                default:
                    break;
            }
            DataContext = viewModel;
            if (order != null)
            {
                //根据测试指令内容更新界面信息
                Enum.TryParse(order.temp, out CaiJiMoKuaiEnum.TestMode mode);
                CaiJiMoKuai parameter = JsonConvert.DeserializeObject<CaiJiMoKuai>(order.deviceModel);
                switch (mode)
                {
                    case CaiJiMoKuaiEnum.TestMode.温湿度:
                        viewModel.MenuIndex = 0;
                        viewModel.Channel = parameter.通道;
                        viewModel.Min = parameter.下限值?.ToString();
                        viewModel.Max = parameter.上限值?.ToString();
                        break;
                    default:
                        break;
                }
            }
        }
        //仪器对象
        public Interfaces.ICaiJiMoKuai Device;
        private void ReadClick(object sender, RoutedEventArgs e)
        {
            double? min = null; double? max =null;
            if (!DataConvert.GetDouble(viewModel.Max,ref max))
            {
                View.Controls.Dialogs.TipView.Show("范围输入有误!");
                return;
            }
            if (!DataConvert.GetDouble(viewModel.Min, ref min))
            {
                View.Controls.Dialogs.TipView.Show("范围输入有误!");
                return;
            }
            //创建参数
            CaiJiMoKuai parameter = new CaiJiMoKuai()
            {
                mode = CaiJiMoKuaiEnum.TestMode.温湿度,
                上限值 = max,
                下限值 = min,
                通道 = viewModel.Channel,
            };
            if (parameter.MinMaxCheck() == false)
            {
                View.Controls.Dialogs.TipView.Show("范围输入有误!");
                return;
            }
            //根具当前打开的窗口调用对应方法
            var debug = Tool.CommonHelper.GetDebugView(this);
            if (debug != null && Device != null)
            {
                //调试功能    
                Task.Run(() => parameter.GetResult(Device, null));
            }
            var edit = Tool.CommonHelper.GetEditView(this);
            if (edit != null && Device != null)
            {
                //生成指令
                edit.InsertOrder(parameter.GetOrder(Device.Name));
            }
        }
    }
}
